package cn.edu.scau.cmi.ema.controller;

import java.util.Set;
import java.util.List;
import cn.edu.scau.cmi.ema.util.CmiHibernateProxyUtil;
import cn.edu.scau.cmi.ema.dao.*;
import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.service.*;
import cn.edu.scau.cmi.ema.service.pagination.*;
import cn.edu.scau.cmi.ema.controller.base.CmiEmaController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.*;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;

@Controller("SubjectController")
public class SubjectController extends CmiEmaController {

	@RequestMapping("/listSubjects")
	public ModelAndView listSubjects(HttpServletRequest request, @ModelAttribute CmiPagination pagination) {
		ModelAndView mav = new ModelAndView();

		pagination = subjectPaginationService.updatePagination(request, pagination);
		Set<Subject> subjects = subjectPaginationService.getPaginationSubjects(pagination);

		mav.addObject("subjects", subjects);
		mav.addObject("entityName", "subject");
		mav.addObject("pagination", pagination);
		mav.setViewName("subject/listSubjects.jsp");
		return mav;
	}

	public String listSubjects() {
		return "redirect:/listSubjects";
	}

	@RequestMapping("/viewSubject")
	public ModelAndView viewSubject(@RequestParam Integer subjectId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("subject", subjectDAO.findSubjectByPrimaryKey(subjectId));
		mav.setViewName("subject/viewSubject.jsp");
		return mav;
	}

	@RequestMapping(value = "/confirmDeleteSubjects")
	public ModelAndView confirmDeleteSubjects(
			@RequestParam(value = "deleteSubjectsIds", required = true) Set<Integer> deleteSubjectsIds) {
		ModelAndView mav = new ModelAndView();

		Set<Subject> deleteSubjects = subjectDAO.findSubjectsByIds(deleteSubjectsIds);
		Set<Subject> referencedSubjects = subjectService.loadReferenceSubjects(deleteSubjects);
		boolean hasReferenced;
		if (referencedSubjects.size() == 0) {
			mav.addObject("deleteEntitys", deleteSubjects);
			hasReferenced = false;
		} else {
			deleteSubjects.removeAll(referencedSubjects);
			mav.addObject("referencedEntitys", referencedSubjects);
			mav.addObject("deleteEntitys", deleteSubjects);
			hasReferenced = true;
		}
		mav.addObject("entityName", "Subject");
		mav.addObject("hasReferenced", hasReferenced);
		mav.setViewName("forward:/listSubjects");
		return mav;
	}

	@RequestMapping("/deleteSubject")
	public String deleteSubject(@RequestParam Integer subjectId) {
		Subject subject = subjectDAO.findSubjectByPrimaryKey(subjectId);
		subjectService.deleteSubject(subject);
		return "forward:/listSubjects";
	}

	@RequestMapping("/confirmDeleteSubject")
	public ModelAndView confirmDeleteSubject(@RequestParam Integer subjectId) {

		ModelAndView mav = new ModelAndView();

		mav.addObject("subject", subjectDAO.findSubjectByPrimaryKey(subjectId));

		mav.setViewName("subject/deleteSubject.jsp");

		return mav;
	}

	@RequestMapping("/deleteSubjects")
	public String deleteSubjects(
			@RequestParam(value = "deleteSubjectsId", required = true) Set<Integer> deleteSubjectsId) {
		subjectService.deleteSubjects(deleteSubjectsId);
		return "forward:/listSubjects";
	}

	@RequestMapping("/editSubject")
	public ModelAndView editSubject(@RequestParam Integer subjectId) {

		ModelAndView mav = new ModelAndView();

		Subject subject = subjectDAO.findSubjectByPrimaryKey(subjectId);
		// 将需要编辑的对象加到mav对象中
		mav.addObject("subject", subject);

		// 把所有的引用subject对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(subjectService.loadSubjectObjectReferencedSet(subject));

		// 将编辑页面subject添加到mav中
		mav.setViewName("subject/editSubject.jsp");
		return mav;
	}

	@RequestMapping("/newSubject")
	public ModelAndView newSubject() {
		ModelAndView mav = new ModelAndView();
		Subject subject = new Subject();

		mav.addObject("subject", subject);

		// 把所有的引用subject对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(subjectService.loadSubjectObjectReferencedSet(subject));

		mav.addObject("newFlag", true);
		mav.setViewName("subject/editSubject.jsp");

		return mav;
	}

	@RequestMapping("/saveSubject")
	public ModelAndView saveSubject(@ModelAttribute Subject subject) {

		ModelAndView mav = new ModelAndView();

		Subject existingSubject = CmiHibernateProxyUtil
				.deproxy(subjectDAO.findSubjectByPhysicalPrimaryKey(subject.getName()));

		// 新建记录
		if (subject.getId() == null) {
			if (subject.equals(existingSubject)) {

				mav.addAllObjects(subjectService.loadSubjectClassReferencedSet(subject));
				// mav = editContinue(mav, subject);
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("subject/editSubject.jsp");
			} else {
				subjectService.saveSubject(subject);
				mav.setViewName("forward:/listSubjects");
			}
		}
		// 编辑记录
		else {
			Subject originSubject = CmiHibernateProxyUtil.deproxy(subjectDAO.findSubjectByPrimaryKey(subject.getId()));
			if (!subject.equals(originSubject) && subject.equals(existingSubject)) {
				mav.addAllObjects(subjectService.loadSubjectClassReferencedSet(subject));
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("subject/editSubject.jsp");
			} else {
				subjectService.saveSubject(subject);
				mav.setViewName("forward:/listSubjects");
			}
		}
		return mav;
	}

	@RequestMapping("/subjectController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request,
			@ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

}
